<?php
class Tourism_goods_profit_model extends CI_Model
{
    private $table = 'tourism_goods_profit';
    private $table_2 = 'user_type';
    
    public function findGoodsProfit($data)
    {
        $this->db->select('*');
        $this->db->from($this->table.' AS tourism_goods_profit');
        $this->db->join($this->table_2.' AS user_type','user_type.type_id=tourism_goods_profit.bit_trade', 'inner');
        if (!empty($data['goods_attr_id'])) {
            $this->db->where('tourism_goods_profit.goods_attr_id', $data['goods_attr_id']);
        }
        if (!empty($data['status'])) {
            $this->db->where('tourism_goods_profit.status', $data['status']);
        }
        $this->db->order_by('tourism_goods_profit.bit_trade ASC');
        return $this->db->get();
    }
    
    public function add($postData, $id)
    {
        $data = array();
        foreach ($postData['amount'] as $key=>$value) {
            if (intval($value) > 0) {
                $data[] = array(
                        'tourism_goods_id'  => $id,
                        'amount'    => trim($value),
                        'uid'       => trim($postData['uid'][$key]),
                        'status'    => trim($postData['status']),
                        'bit_trade' => trim($postData['type_id'][$key])
                );
            }
        }
        return $this->db->insert_batch($this->table, $data);
    }
    
    public function bargainingDelete($allot_id, $status)
    {
        $this->db->where('tourism_goods_id', $allot_id);
        $this->db->where('status', $status);
        return $this->db->delete($this->table);
    }
    
}